package com.luojialong.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luojialong.model.domain.ProjectTb;
import com.luojialong.model.request.projectTb.ProjectTbAddRequest;
import com.luojialong.model.request.projectTb.ProjectTbQueryRequest;
import com.luojialong.model.vo.projectTb.ProjectTbPageVo;
import com.luojialong.service.ProjectTbService;
import com.luojialong.mapper.ProjectTbMapper;
import com.luojialong.utils.MyStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * @description 针对表【project_tb】的数据库操作Service实现
 * @createDate 2024-04-23 20:21:06
 */
@Service
public class ProjectTbServiceImpl extends ServiceImpl<ProjectTbMapper, ProjectTb>
        implements ProjectTbService {

    @Override
    public ProjectTb getAddProjectTb(ProjectTbAddRequest projectTbAddRequest) {
        ProjectTb projectTb = new ProjectTb();
        BeanUtils.copyProperties(projectTbAddRequest, projectTb);
        return projectTb;
    }

    @Override
    public QueryWrapper<ProjectTb> getQueryWrapper(ProjectTbQueryRequest projectTbQueryRequest) {
        QueryWrapper<ProjectTb> queryWrapper = new QueryWrapper<>();
        String sortField = projectTbQueryRequest.getSortField();
        String sortOrder = projectTbQueryRequest.getSortOrder();
        MyStringUtils.setWrapper(queryWrapper, sortField, sortOrder);
        Long id = projectTbQueryRequest.getId();
        if (id != null) {
            queryWrapper.eq("id", id);
        }
        String title = projectTbQueryRequest.getTitle();
        if (title != null) {
            queryWrapper.like("title", title);
        }
        String category = projectTbQueryRequest.getCategory();
        if (category != null) {
            queryWrapper.like("category", category);
        }
        String lesson = projectTbQueryRequest.getLesson();
        if (lesson != null) {
            queryWrapper.like("lesson", lesson);
        }
        Integer isStore = projectTbQueryRequest.getIsStore();
        if (isStore != null) {
            queryWrapper.eq("isStore", isStore);
        }
        Long teacherId = projectTbQueryRequest.getTeacherId();
        if (teacherId != null) {
            queryWrapper.eq("teacherId", teacherId);
        }
        String teachName = projectTbQueryRequest.getTeachName();
        if (teachName != null) {
            queryWrapper.like("teachName", teachName);
        }

        return queryWrapper;
    }

    @Override
    public Page<ProjectTbPageVo> getProjectTbVo(Page<ProjectTb> projectTbPage) {
        List<ProjectTb> records = projectTbPage.getRecords();
        List<ProjectTbPageVo> collect = records.stream().map(ProjectTbPageVo::transferVo).collect(Collectors.toList());
        Page<ProjectTbPageVo> projectTbVoPage = new Page<>(projectTbPage.getCurrent(),projectTbPage.getSize(),projectTbPage.getTotal());
        projectTbVoPage.setRecords(collect);
        return projectTbVoPage;
    }

    @Override
    public Boolean del(Long id) {
        return this.removeById(id);
    }
}




